package com.shundr.shipper.cargo.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.cargo.http.CargoRequest;
import com.shundr.shipper.cargo.model.MyCargoInfo;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.DialogUtil;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 
 * OrderItemAdapter 订单列表适配器
 * 
 * @author larry 2015-4-13 下午4:26:47
 * @version 1.0.0
 * 
 */
public class MyCargoAdapter extends BaseAdapter {
	public List<MyCargoInfo> mList;
	private Context mContext;

	private OnOffShelfListener offShelfListener;
	
	private DeleteListener deleteListener;
	
	public void setDeleteListener(DeleteListener listener) {
		deleteListener = listener;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public MyCargoAdapter(List<MyCargoInfo> mlist, Context mContext) {
		super();
		this.mList = mlist;
		this.mContext = mContext;
	}

	@Override
	public View getView(final int position, View convertView,
			ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_my_cargo, null);
			viewHold = new ViewHold();
			viewHold.tvFrom = (TextView) convertView.findViewById(R.id.tv_from);
			viewHold.tvTo = (TextView) convertView.findViewById(R.id.tv_to);
			viewHold.tvType = (TextView) convertView
					.findViewById(R.id.tv_cargo_type);
			viewHold.tvWeight = (TextView) convertView
					.findViewById(R.id.tv_cargo_weight);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.tvStatus = (TextView) convertView
					.findViewById(R.id.tv_status);
			viewHold.btnDelete = (Button) convertView
					.findViewById(R.id.btn_delete);
			viewHold.btnResend = (Button) convertView
					.findViewById(R.id.btn_resend);
			viewHold.btnOffShelf = (Button) convertView
					.findViewById(R.id.btn_off_shelf);
			convertView.setTag(viewHold);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			final MyCargoInfo info = mList.get(position);
			if (StringUtil.isNull(info.getCargoSrcPlace())) {
				viewHold.tvFrom.setText("起点未知");
			} else {
				viewHold.tvFrom.setText(info.getCargoSrcPlace());
			}
			if (StringUtil.isNull(info.getCargoDstPlace())) {
				viewHold.tvTo.setText("终点未知");
			} else {
				viewHold.tvTo.setText(info.getCargoDstPlace());
			}
			viewHold.tvType.setText(info.getCargoCategory());
			viewHold.tvWeight.setText(info.getCargoAmount() + " "
					+ info.getCargoUnitName());
			viewHold.tvTime.setText(DateUtil.getInterval(info
					.getCargoResendTime()));

			if (info.isResend()) {
				viewHold.btnResend.setBackgroundResource(R.drawable.item_common_gray_selector);
				viewHold.btnResend.setClickable(false);
			} else {
				viewHold.btnResend.setBackgroundResource(R.drawable.item_common_shape_selector);
				viewHold.btnResend.setClickable(true);
			}
			
			if (StringUtil.isEqual(info.getCargoStatus(), "1")) {
				viewHold.tvStatus.setText("待成交");
				viewHold.btnResend.setVisibility(View.VISIBLE);
				viewHold.btnOffShelf.setVisibility(View.VISIBLE);
			} else {
				viewHold.tvStatus.setText("已下架");
				viewHold.btnResend.setVisibility(View.GONE);
				viewHold.btnOffShelf.setVisibility(View.GONE);
			}
			
			viewHold.btnDelete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					DialogUtil.noticeAlertDialog(mContext, "删除", "是否确定删除此货源？",
							new OnDisplayListener() {

								@Override
								public void confirm() {
									
									if (deleteListener != null) {
										deleteListener.deleteAt(position);
									} else {
										ToastHelper.toastShort(mContext, "删除出现异常！");
									}
									
//									new CargoRequest(mContext, new Handler() {
//										@Override
//										public void handleMessage(Message msg) {
//											switch (msg.what) {
//											case CargoRequest.DELETE_CARGO_SUCCESSED:
//												try {
//													ToastHelper.toastShort(
//															mContext, "已删除");
//													new Handler().postDelayed(
//															new Runnable() {
//
//																@Override
//																public void run() {
//																	mList.remove(position);
//																	notifyDataSetChanged();
//																}
//															}, 0);
//												} catch (Exception e) {
//													e.printStackTrace();
//												}
//												break;
//											case LHttpRequest.REQUEST_FAILED:
//												try {
//													ToastHelper.toastShort(mContext,msg.obj.toString());
//												} catch (Exception e) {
//													e.printStackTrace();
//												}
//												break;
//											default:
//												break;
//											}
//											super.handleMessage(msg);
//										}
//									}).deleteMyCargo(info.getCargoId());
								}

								@Override
								public void cancel() {

								}
							});
					
				}
			});

			viewHold.btnResend.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(final View v) {
					new CargoRequest(mContext, new Handler() {
						@Override
						public void handleMessage(Message msg) {
							switch (msg.what) {
							case CargoRequest.RESEND_CARGO_SUCCESSED:
								try {
									ToastHelper.toastShort(mContext,msg.obj.toString());
									info.setResend(true);
									new Handler().postDelayed(new Runnable() {

										@Override
										public void run() {
											notifyDataSetChanged();
										}
									}, 0);
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							case LHttpRequest.REQUEST_FAILED:
								try {
									ToastHelper.toastShort(mContext,msg.obj.toString());
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							default:
								break;
							}
							super.handleMessage(msg);
						}
					}).resendCargo(info.getCargoId());
				}
			});

			viewHold.btnOffShelf.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					DialogUtil.noticeAlertDialog(mContext, "下架", "是否确定下架此货源？",
							new OnDisplayListener() {

								@Override
								public void confirm() {
									new CargoRequest(mContext, new Handler() {
										@Override
										public void handleMessage(Message msg) {
											switch (msg.what) {
											case CargoRequest.OFF_SHELF_CARGO_SUCCESSED:
												try {
													ToastHelper.toastShort(
															mContext, "已下架");
													new Handler().postDelayed(
															new Runnable() {

																@Override
																public void run() {
																	mList.remove(position);
																	notifyDataSetChanged();
																}
															}, 0);
												} catch (Exception e) {
													e.printStackTrace();
												}
												break;
											case LHttpRequest.REQUEST_FAILED:
												try {
													ToastHelper.toastShort(mContext,msg.obj.toString());
												} catch (Exception e) {
													e.printStackTrace();
												}
												break;
											default:
												break;
											}
											super.handleMessage(msg);
										}
									}).offShelfCargo(info.getCargoId());
								}

								@Override
								public void cancel() {

								}
							});

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHold {
		TextView tvFrom;
		TextView tvTo;
		TextView tvType;
		TextView tvWeight;
		TextView tvTime;
		TextView tvStatus;

		Button btnDelete;
		Button btnResend;
		Button btnOffShelf;
	}

	public void setOffShelfListener(OnOffShelfListener offShelfListener) {
		this.offShelfListener = offShelfListener;
	}

	public interface OnOffShelfListener {
		public void onOffed();
	}
	
	public interface DeleteListener {
		public void deleteAt(int position);
	}
}
